﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Tools;
using System.Windows.Forms;

namespace vbReturn.classes
{
    public class clsRGAUpload_List
    {
        private List<clsRGAUploadHeader> list;

        public clsRGAUpload_List()
        {
            this.list = new List<clsRGAUploadHeader>();
        }

        public bool gotRecord()
        {
            bool bResult = true;

            if (this.list == null)
            {
                bResult = false;
            }
            else if (this.list.Count() == 0)
            {
                bResult = false;
            }

            return bResult;
        }
        
        public clsRGAUploadHeader getHeaderByIndex(int index)
        {
            if (gotRecord())
            {
                return this.list[index];
            }
            return null;
        }

        public clsRGAUploadHeader getHeaderByExternReceiptKey(string sExReceiptKey)
        {
            clsRGAUploadHeader result = null;
            
            foreach (clsRGAUploadHeader header in list)
            {
                if (header.SReceiptKey == sExReceiptKey)
                {
                    result = header;
                }
            }

            return result;
        }

        public void addRGAHeader(clsRGAUploadHeader clsInput)
        {
            this.list.Add(clsInput);
        }

        public int count()
        {
            if (!gotRecord())
            {
                return 0;
            }

            return this.list.Count();
        }

        public string writeToCSV(clsRGAUploadHeader clsRGAHeader, string sDir, bool bChooseDir)
        {
            string sErrorMsg = null;
            
            DateTime currentTime = DateTime.Now;

            
            string sFileName = clsRGAHeader.SReturnID + "_" + clsRGAHeader.SReceiptKey + "_" + currentTime.ToString("yyyyMMddHHmmss") + ".csv";

            using (var writer = new CsvFileWriter(sFileName))
            {

                List<string> headerH = new List<string>();
                headerH.Add("HEADER");
                headerH.Add("EXTERNRECEIPTKEY");
                headerH.Add("ASNTYPE");
                headerH.Add("WAREHOUSEREFERENCE");
                headerH.Add("CARRIERKEY");

                List<string> headerL = new List<string>();
                headerL.Add("DETAIL");
                headerL.Add("EXTERNRECEIPTKEY");
                headerL.Add("EXTERNLINENUMBER");
                headerL.Add("SKU");
                headerL.Add("QTYEXPECTED");
                headerL.Add("UOM");
                headerL.Add("TOLOC");
                headerL.Add("LOTTABLE05");

                writer.WriteRow(headerH);
                writer.WriteRow(headerL);

                
                List<string> header = new List<string>();
                header.Add("0");
                header.Add(clsRGAHeader.SReceiptKey);
                header.Add(clsRGAHeader.SASNType);
                header.Add(clsRGAHeader.SWarehouseRef);
                header.Add(clsRGAHeader.SCarrierKey);

                writer.WriteRow(header);

                for (int j = 0; j < clsRGAHeader.getNoOfDetailLines(); j++)
                {
                    clsRGAUploadDetail detail = clsRGAHeader.getRGALineByIndex(j);

                    List<string> line = new List<string>();
                    line.Add("1");
                    line.Add(clsRGAHeader.SReceiptKey);
                    line.Add(detail.SExternLineNo);
                    line.Add(detail.SSKU);
                    line.Add(detail.IQtyExpected.ToString());
                    line.Add(detail.SUOM);
                    line.Add(detail.SToLoc);

                    //format to Choon Leong's format
                    DateTime dLot05 = Convert.ToDateTime((detail.SLottable05));
                    string sTemp = dLot05.ToString("d/MM/yyyy H:mm");
                    line.Add(dLot05.ToString("d/MM/yyyy H:mm"));

                    writer.WriteRow(line);

                }


                writer.Dispose();
            }

          
            if (bChooseDir)
            {
                sErrorMsg = copyFileToChosenDir(sFileName, sDir);
            }
            else
            {
                sErrorMsg = copyFileToSeeBurger(sFileName);
            }
            
            return sErrorMsg;
        }

        public string copyFileToChosenDir(string sFileName, string sDir)
        {
            string sErrorMsg = null;

            string sCurrentFilePath = Directory.GetCurrentDirectory() + @"\" + sFileName;
            string sNewFilePath = sDir + @"\" + sFileName;

            try
            {
                
                // code that executes under the new context.
                File.Copy(sCurrentFilePath, sNewFilePath);
                
            }
            catch (Exception ex)
            {
                sErrorMsg = ex.Message;
            }

            return sErrorMsg;
        }

        public string copyFileToSeeBurger(string sFileName)
        {
            string sErrorMsg = null;

            string sCurrentFilePath = Directory.GetCurrentDirectory() + @"\" + sFileName;
            string sNewFilePath = @"\\" + clsConfig.SSeeBurgerRGAPath + sFileName;

            try
            {
                try
                {

                   File.Copy(sCurrentFilePath, sNewFilePath);   
                   /*
                   using (new Impersonator(clsConfig.SSeeBurgerID, clsConfig.SSeeBurgerDomain, clsConfig.SSeeBurgerPassword))
                   {
                       // code that executes under the new context.
                       File.Copy(sCurrentFilePath, sNewFilePath);
                   }
                   */
               
                }
                catch (Exception ex)
                {
                    File.Copy(sCurrentFilePath, sNewFilePath);
                }
                
                

                if (clsConfig.BDeleteRGAExcelFile)
                {
                    File.Delete(sCurrentFilePath);
                }

            }
            catch (Exception ex)
            {
                sErrorMsg = ex.Message;
            }

            return sErrorMsg;
        }
    }
}
